package com.example.xianji.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.example.xianji.R;
import com.example.xianji.bean.ZhanHui_Bean;
import com.example.xianji.utils.ImageLoadUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ZhanHui_Adapter extends BaseAdapter {
    /*****************展会大厅列表适配器**********************************/
    private ArrayList<ZhanHui_Bean> list;

    private Context context;
    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
    private DisplayImageOptions options;

    public ZhanHui_Adapter(ArrayList<ZhanHui_Bean> list, Context context) {
        super();
        this.list = list;
        this.context = context;
        options = new DisplayImageOptions.Builder()
                /*.showImageOnLoading(R.drawable.touxiang)
                .showImageForEmptyUri(R.drawable.touxiang)
                .showImageOnFail(R.drawable.touxiang)*/
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Config.RGB_565)
                .delayBeforeLoading(100)
                .displayer(new RoundedBitmapDisplayer(20)).build();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder mHolder;
        if (convertView == null) {
            mHolder = new Holder();
            convertView = View.inflate(context, R.layout.zhanhui_list_item, null);
            mHolder.zhanhui_image = (ImageView) convertView.findViewById(R.id.zhanhui_image);
            mHolder.zhanhui_time = (TextView) convertView.findViewById(R.id.zhanhui_time);
            mHolder.zhanhui_place = (TextView) convertView.findViewById(R.id.zhanhui_place);
            mHolder.zhanhui_title = (TextView) convertView.findViewById(R.id.zhanhui_title);
            convertView.setTag(mHolder);
        }
        mHolder = (Holder) convertView.getTag();
        ImageViewAware iamgeviewaware = new ImageViewAware(mHolder.zhanhui_image, false);
        //清楚imageView设置的图片和从Bitmap 缓存内存中获取
        ImageLoadUtil.loadImageByPath(options, list.get(position).getImge(), iamgeviewaware, animateFirstListener);
        mHolder.zhanhui_time.setText(new StringBuffer("开展时间：  ").append(list.get(position).getDate()));
        mHolder.zhanhui_place.setText(new StringBuffer("展会场馆：  ").append(list.get(position).getPlace()));
        mHolder.zhanhui_title.setText(list.get(position).getTitle());
        return convertView;
    }

    class Holder {
        TextView zhanhui_time, zhanhui_place, zhanhui_title;
        ImageView zhanhui_image;

    }

    public static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
        public static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 200);
                    displayedImages.add(imageUri);
                }
            }
        }
    }
}
